package leetcode;

/**
 * @author:xufengxiang
 * @date 2023-03-16 19:06
 */
public class Code14 {

    public static String longestCommonPrefix(String[] strs) {
        int strsLength = strs.length;
        if(strsLength <=1){
            return strs[0];
        }

        StringBuilder sb = new StringBuilder("");

        int minStrLength = strs[0].length();
        for(int i=1;i<strsLength;i++){
            minStrLength = Math.min(strs[i].length(), minStrLength);
        }

        if(minStrLength ==0){
            return "";
        }

        for(int i=0;i< minStrLength;i++){
            char c = strs[0].charAt(i);
            for(int j=1;j< strsLength;j++){
                char c1 = strs[j].charAt(i);
                if(c != c1){
                    return sb.toString();
                }
            }
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"flower","flow","flight"}));
    }
}
